﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Data.Json;
using Xiaowei.Helpers;
using Xiaowei.Services;
using Xiaowei.Settings;

namespace Xiaowei.Models
{
    public class WarrantyItem
    {
        public string WarrantyType { get; set; }
        public string ServiceType { get; set; }
        public bool State { get; set; }
        public string StateText { get; set; }
        public string StartTime { get; set; }
        public string EndTime { get; set; }
    }
    public class WarrantyModel
    {
        public bool State { get; set; } = false;
        public string StateText { get; set; } = "";
        public string PN { get; set; } = "";
        public string SN { get; set; } = "";
        public string Content { get; set; } = "";
        public LoadingStateEnum LoadingState { get; set; } = LoadingStateEnum.Loading;
        public enum LoadingStateEnum
        {
            Loading,
            Success,
            Fail,
            NoSupport
        }
        public List<WarrantyItem> ObligationList { get; } = new List<WarrantyItem>();
        public event Action FetchWarrantyMessageEnded;
        public void FetchWarrantyMessage()
        {
            string pn = "CZ993A";
            string sn = "TH85V6800X";
            if (!DebugConfig.TestDriverList)
            {
                pn = LocalSettings.ProductNumber;
                sn = LocalSettings.SerialNumber;
            }
            Task.Run(async () =>
            {
                try
                {
                    string data = await ServerApi.Instance.GetObligationMessage(pn, sn).ConfigureAwait(false);
                    JsonObject jo = JsonObject.Parse(data);
                    if (jo["code"].GetNumber() == 200)
                    {
                        PN = pn;
                        SN = sn;
                        State = false;
                        StateText = "已过保修期";
                        JsonArray ja = jo["data"].GetArray();
                        foreach (var item in ja)
                        {
                            JsonObject warrantObject = item.GetObject();
                            WarrantyItem warrantyItem = new WarrantyItem();
                            warrantyItem.WarrantyType = warrantObject.GetNamedString("serviceObligationType", "");
                            warrantyItem.ServiceType = warrantObject.GetNamedString("serviceObligationDesc", "");
                            warrantyItem.StartTime = warrantObject.GetNamedString("startTime", "");
                            warrantyItem.EndTime = warrantObject.GetNamedString("endTime", "");
                            warrantyItem.State = false;
                            warrantyItem.StateText = "已过保修期";
                            DateTime now = DateTime.Now;
                            if (DateTime.TryParse(warrantyItem.EndTime, out DateTime endTime))
                            {
                                if (endTime > now)
                                {
                                    State = true;
                                    StateText = "活跃状态中";
                                    warrantyItem.State = true;
                                    int totalMonth = (endTime.Year - now.Year) * 12 + (endTime.Month - now.Month) - (endTime.Day < now.Day ? 1 : 0);
                                    if (totalMonth == 0)
                                    {
                                        warrantyItem.StateText = $"活跃状态中 剩余{(endTime - now).Days}天";
                                    }
                                    else
                                    {
                                        warrantyItem.StateText = $"活跃状态中 剩余{totalMonth}个月";
                                    }
                                }
                            }
                            ObligationList.Add(warrantyItem);
                        }
                        LoadingState = LoadingStateEnum.Success;
                    }
                    else if (jo["code"].GetNumber() == 600)
                    {
                        LoadingState = LoadingStateEnum.NoSupport;
                    }
                    else
                    {
                        LoadingState = LoadingStateEnum.Fail;
                    }
                    FetchWarrantyMessageEnded?.Invoke();
                    FetchWarrantyMessageEnded = null;
                }
                catch
                {
                    LoadingState = LoadingStateEnum.Fail;
                    FetchWarrantyMessageEnded?.Invoke();
                    FetchWarrantyMessageEnded = null;
                }
                //MiniModeService.Instance.UpdateMiniUI(this);
            });
        }
    }
}
